const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {

    talkPageSize:5,
    markPageSize:5,
    stars: [0, 1, 2, 3, 4],
    normalSrc: '/images/@2x_star_b.png',
    selectedSrc: '/images/@2x_star_y.png',
    id: '',
    user_id:'',
    orderId:'1',
    orderList:[
      {
        id: 1,
        name: "最新发表"
      },
      {
        id: 2,
        name: "最后回复"
      },
      {
        id: 3,
        name: "回复数量"
      }
    ],
    courseCategoryList: [{
        id: 1,
        name: "课程详情"
      },
      {
        id: 2,
        name: "课程列表"
      },
      {
        id: 3,
        name: "名师介绍"
      },
      {
        id: 4,
        name: "课堂交流"
      },
      {
        id: 5,
        name: "课堂评价"
      }
    ],
    course: null,
    courseMark: [],
    consult: null,
    level: '',
    categoryId: '',
    huifu_show: -1,
    courseTalk: [],
    replytotal: '',
    marktotal: '',
    like:''
  },
  moreTalk(){
    let that=this
    that.setData({
      talkPageSize: that.data.talkPageSize+5
    })
    wx.request({
      url: 'http://10.10.18.194:8888/sfzy/CourseTalk/findByCourseId',
      data: {
        course_id: that.data.course.id,
        pageSize:that.data.talkPageSize
      },
      success(res) {
        let result = res.data.data;
        //console.log(result.list)
        that.setData({
          courseTalk: result.list
        })
      }
    })
  },
  moreMark(){
    let that=this
    that.setData({
      markPageSize: that.data.markPageSize+999
    })
    wx.request({
      url: 'http://10.10.18.194:8888/sfzy/CourseMark/findByCourseId',
      data: {
        course_id: that.data.course.id,
        pageSize:that.data.markPageSize
      },
      success(res) {
        let result = res.data.data;
        //console.log(result)
        that.setData({
          marktotal:result.list.length,
          courseMark: result.list
        })
        //console.log(that.data.marktotal)
      }
    })
  },
  toConsult(even) {
    let consultId = this.data.consult.id;
    //console.log(consultId)
    wx.navigateTo({
      url: '/pages/consult/detail/detail?id=' + consultId,
    })
  },
  addLike(){
    let that=this
    if(that.data.like==0){

      wx.request({
        url: 'http://10.10.18.194:8888/sfzy/UserCourse/updatejia?id='+app.globalData.point.id,
        success(res) {
          console.log(app.globalData.point.id)
          console.log("积分加一")
        },
      })
    }else{
      wx.request({
        url: 'http://10.10.18.194:8888/sfzy/UserCourse/updatejian?id='+app.globalData.point.id,
        success(res) {
          console.log(app.globalData.point.id)
          console.log("取消了就没了哦")
        },
      })
    }

    if(that.data.like==0){
      wx.request({
        url: 'http://10.10.18.194:8888/sfzy/UserCourse/add',
        method:"post",
        header: {
          "Content-Type": "application/x-www-form-urlencoded;charset=UTF-8"
        },
        data: {
          user_id:app.globalData.point.id,
          course_id: that.data.id
        },
        success(res) {
          console.log("收藏成功")
          that.setData({
            like: 1
          })
        },
      })
    }else{
      wx.request({
        url: 'http://10.10.18.194:8888/sfzy/UserCourse/deleteByUserAndCourse',
        method:"post",
        header: {
          "Content-Type": "application/x-www-form-urlencoded;charset=UTF-8"
        },
        data: {
          user_id:app.globalData.point.id,
          course_id: that.data.id
        },
        success(res) {
          console.log("取消收藏")
          that.setData({
            like: 0
          })
        },
      })
    }
  },
  toAccess(even) {
    let courseId = this.data.course.id;
    //console.log(courseId)
    wx.navigateTo({
      url: '/pages/course/access/access?id='+courseId,
    })
  },
  toZXYY(even) {
    wx.navigateTo({
      url: '/pages/index/zxyy/zxyy',
    })
  },
  changeActive(event) {
    let categoryId = event.currentTarget.id;
    this.setData({
      categoryId: categoryId * 1
    })
  },
  changeOrder(event) {
    let that = this
    let orderId = event.currentTarget.id;
    that.setData({
      orderId: orderId * 1
    })
    if(orderId==1){
      wx.request({
        url: 'http://10.10.18.194:8888/sfzy/CourseTalk/findByCourseId',
        data: {
          course_id: that.data.course.id
        },
        success(res) {
          let result = res.data.data;
          //console.log(result.list)
          that.setData({
            courseTalk: result.list
          })
        }
      })
    }else if(orderId==2){
      wx.request({
        url: 'http://10.10.18.194:8888/sfzy/CourseTalk/findByCourseIdOrder1',
        data: {
          course_id: that.data.course.id
        },
        success(res) {
          let result = res.data.data;
          that.setData({
            courseTalk: result
          })
        }
      })
    }else if(orderId==3){
      wx.request({
        url: 'http://10.10.18.194:8888/sfzy/CourseTalk/findByCourseIdOrder2',
        data: {
          course_id: that.data.course.id
        },
        success(res) {
          let result = res.data.data;
          that.setData({
            courseTalk: result
          })
        }
      })
    }
  },
  changehuifu(event) {
    if (this.data.huifu_show == -1) {
      this.setData({
        huifu_show: event.currentTarget.dataset.num
      })
    } else {
      this.setData({
        huifu_show: -1
      })
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    if(app.globalData.userInfo==null){
      wx.showModal({
        title: '提示',
        content: '请先登录',
        complete: (res) => {
          if (res.confirm) {
            wx.navigateTo({
              url: '/pages/my/login/login',
            })
          }
        }
      })
    }else{
        // this.getData();
    }
    let that = this;
    that.setData({
      id: options.id,
      categoryId: this.data.courseCategoryList[0].id
    })
    wx.request({
      url: 'http://10.10.18.194:8888/sfzy/UserCourse/findIdByUserAndCourse',
      data: {
        user_id:app.globalData.point.id,
        course_id: that.data.id
      },
      success(res) {
        let result = res.data.data;
        //console.log(result)
        that.setData({
          like:result
        })
      },
    })
    wx.request({
      url: 'http://10.10.18.194:8888/sfzy/Course/findById',
      data: {
        id: that.data.id
      },
      success(res) {
        let result = res.data.data;
        that.setData({
          course: result
        })
        wx.request({
          url: 'http://10.10.18.194:8888/sfzy/CourseTalk/findByCourseId',
          data: {
            course_id: that.data.course.id
          },
          success(res) {
            let result = res.data.data;
            //console.log(result.list)
            that.setData({
              courseTalk: result.list
            })
          }
        })
        wx.request({
          url: 'http://10.10.18.194:8888/sfzy/consult/findById/' + that.data.course.teacher_id,
          success(res) {
            let result = res.data.data;
            that.setData({
              consult: result[0]
            })
            wx.request({
              url: 'http://10.10.18.194:8888/sfzy/CourseMark/findByCourseId',
              data: {
                course_id: that.data.course.id
              },
              success(res) {
                let result = res.data.data;
                //console.log(result)
                that.setData({
                  marktotal:result.list.length,
                  courseMark: result.list
                })
                //console.log(that.data.marktotal)
              }
            })
          }
        })
      }
    })


  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})